package net.lainiao.mycost.service.impl;

import net.lainiao.mycost.bean.CostInfo;
import net.lainiao.mycost.dao.ICostInfoDAO;
import net.lainiao.mycost.service.ICostInfoService;
import net.lainiao.mycost.utils.Comm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */
@Service
@Transactional
public class CostInfoServiceImpl implements ICostInfoService {
    @Autowired
    private ICostInfoDAO costInfoDAO;

    @Override
    public void add(CostInfo info) {
        costInfoDAO.add(info);
    }

    @Override
    public List<CostInfo> list(CostInfo info) {
        condition(info);
        return costInfoDAO.list(info);
    }

    @Override
    public long getCount(CostInfo info) {
        condition(info);
        return costInfoDAO.getCount(info);
    }

    @Override
    public CostInfo getInfo(CostInfo info) {
        return costInfoDAO.getCostInfo(info);
    }

    @Override
    public void update(CostInfo info) {
        costInfoDAO.update(info);
    }

    @Override
    public void delete(Integer[] costIds) {
        for(Integer costId:costIds){
            CostInfo info=new CostInfo();
            info.setCostId(costId);
            info.setCostMark(Comm.MARK_NO);
            costInfoDAO.update(info);
        }
    }

    private void condition(CostInfo info) {
        if(info!=null){
            if(StringUtils.isNotEmpty(info.getCostName())){
                info.setCostName("%"+info.getCostName()+"%");
            }
        }
    }
}
